//
//  LiuWelcomeViewController.swift
//  LiuWeiBo
//
//  Created by 刘晓晨 on 2017/3/29.
//  Copyright © 2017年 刘晓晨. All rights reserved.
//

import UIKit
import SDWebImage
class LiuWelcomeViewController: UIViewController {

    //懒加载控件
    lazy var userImageView:UIImageView = {
       let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        if let imageStr = LiuUserAccountViewModel.sharedUserAccountViewModel.userAccount?.avatar_large {
            
            imageView.sd_setImage(with: URL(string: imageStr), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        imageView.layer.cornerRadius = 45
        imageView.layer.masksToBounds = true
        
        return imageView
        
    }()
    
    lazy var userMessage:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 13)
        let name = LiuUserAccountViewModel.sharedUserAccountViewModel.userAccount?.name
        if name != nil {
            label.text = "欢迎\(name!)回来!"
        } else {
            label.text = "欢迎回来"
        }
        label.textAlignment = .center
        //设置透明度
        label.alpha = 0
        return label
        
    }()
    
    //自定义view
    override func loadView() {
        let imageView = UIImageView(image: UIImage(named: "ad_background"))
        
        view = imageView
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
    }
    
    fileprivate func setupUI() {
        
        view.addSubview(userImageView)
        view.addSubview(userMessage)
        
        //自动布局
        userImageView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        userMessage.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(userImageView.snp.bottom).offset(10)
        }
    }
    
    //动画
    fileprivate func animationAction() {
        
        userImageView.snp.updateConstraints { (make) in
            make.top.equalTo(view).offset(100)
            
        }
        
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: {
            self.view.layoutIfNeeded()
        }, completion: { (_) in
            UIView.animate(withDuration: 2, animations: {
                self.userMessage.alpha = 1
            }, completion: { (_) in
                
                //发送通知
                NotificationCenter.default.post(name: NSNotification.Name(notificationName), object: self.classForCoder)
            })
        })
        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        animationAction()
    }

   

}
